package com.grao.waterschool.human.manager.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.grao.waterschool.human.common.BusinessException;
import com.grao.waterschool.human.entiry.dto.FileName;
import com.grao.waterschool.human.manager.service.SeetaFaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

@Service
public class SeetaFaceServiceImpl implements SeetaFaceService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResponseEntity<String> callDetectFaceApi(String apiHost, Integer apiPort, @RequestBody FileName fileName) {

        if (!StpUtil.isLogin()) {
            throw new BusinessException("请在登录后重试");
        }

//        String apiUrl = "http://" + apiHost + ":" + apiPort + "/seetaface6/face/detect";
        String apiUrl = "http://" + apiHost + ":" + apiPort + "/seetaface6/face/detect";

        String fileNames = fileName.getFileName();

        ResponseEntity<String> result;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> httpEntity = new HttpEntity<>(fileNames, headers);

        result = restTemplate.postForEntity(apiUrl, httpEntity, String.class);
        return result;
    }
}
